package com.example.juc.pc;

/**
 * 线程通信问题 消费者生产者问题，等待唤醒，通知唤醒
 * 线程交替执行
 */
public class A {

    public static void main(String[] args) {

        Data data = new Data();
        new Thread(()-> {
            try {
                for (int i =0;i<10;i++){
                    data.increment();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();

        new Thread(()-> {
            try {
                for (int i =0;i<10;i++){
                    data.decrement();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"B").start();

        new Thread(()-> {
            try {
                for (int i =0;i<10;i++){
                    data.increment();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"C").start();

        new Thread(()-> {
            try {
                for (int i =0;i<10;i++){
                    data.decrement();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"D").start();
    }
}


class Data{


    private int num =0;

    public synchronized void increment() throws InterruptedException {
       if(num!=0){
           this.wait();
       }
       num++;
       System.out.println(Thread.currentThread().getName()+"->"+num);
       this.notifyAll();
    }

    public synchronized void decrement() throws InterruptedException {
        if (num==0){
            this.wait();
        }
        num--;
        System.out.println(Thread.currentThread().getName()+"->"+num);
        this.notifyAll();
    }
}